//
//  Person.m
//  OC06_集合遍历_数组排序
//
//  Created by Scott on 16/3/3.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "Person.h"

@implementation Person

#pragma mark - Creating and Init Person
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
    
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
        
    }
    return self;
}

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age {
    
    return [[Person alloc] initWithName:name age:age];
}


#pragma mark - Sort
/** 根据姓名降序排序. */
- (NSComparisonResult)DeSortedByName:(Person *)person {
    
    return [self.name compare:person.name] * (-1);
    
}

/** 根据姓名升序排序. */
- (NSComparisonResult)asSortedByName:(Person *)person {
    return [self.name compare:person.name];
}

/** 根据年龄升序排序. */
- (NSComparisonResult)asSortedByAge:(Person *)person {
    
    if (self.age > person.age) {
        
        return NSOrderedDescending;
        
    } else if (self.age < person.age) {
        return NSOrderedAscending;
        
        
    } else {
        return NSOrderedSame;
    }

    
}

/** 根据年龄降序排序. */
- (NSComparisonResult)DesortedbyAge:(Person *)person {
    
    if (self.age > person.age) {
        return NSOrderedAscending;
    } else if (self.age < person.age) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
    
    
}






@end
